package com.china08.yxyapi.api.manage.stu.mystudy.tifenbao.studyrecord;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnow;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnowTrain;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnowTrainQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowTrainQuesRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowTrainRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 学习记录-学习计划详情-查看详情
 * 
 */
@CrossOrigin
@RestController
public class TifenbaoStudyRecordKnowDetailController extends YxyApiBaseController {

    @Autowired
    private YxyHuixuePlanKnowRepository yxyHuixuePlanKnowRepository;
    @Autowired
    private YxyHuixuePlanKnowTrainRepository yxyHuixuePlanKnowTrainRepository;
    @Autowired
    private YxyHuixuePlanKnowTrainQuesRepository yxyHuixuePlanKnowTrainQuesRepository;

    @ResponseBody
    @ApiOperation(value = "学习记录-学习计划详情-查看详情")
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/tifenbao/record/plan/know/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws ParseException {
        RespModel model = new RespModel();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DecimalFormat df = new DecimalFormat("######0.0");
        // 通过id查知识点
        YxyHuixuePlanKnow know = yxyHuixuePlanKnowRepository.findOne(reqModel.id);
        if (know != null) {
            model.status = (know.getTask().intValue() == know.getPassTask().intValue() ? 0 : 1);
            // 总练习题目
            List<YxyHuixuePlanKnowTrainQues> totals = yxyHuixuePlanKnowTrainQuesRepository
                    .findByPlanIdAndKnowId(know.getPlanId(), know.getKnowId());
            int totalcount = totals.size();
            int time = 0;
            for (YxyHuixuePlanKnowTrainQues y : totals) {
                String usetime = y.getUseTime();
                String[] times = usetime.split(":");
                time += Integer.parseInt(times[0]) * 60 * 60 + Integer.parseInt(times[1]) * 60
                        + Integer.parseInt(times[2]);
            }
            String avgtimestr = "";
            int avgtime = time / totalcount;
            int hour = avgtime / 3600;
            int minute = avgtime / 60 % 60;
            int second = avgtime % 60;
            if (hour != 0) {
                avgtimestr += (hour + "时");
            }
            avgtimestr += (minute + "分" + second + "秒");
            model.avgtime = avgtimestr;
            model.quesCount = totalcount;
            // 答对次数
            int passcount = yxyHuixuePlanKnowTrainQuesRepository.countByPlanIdAndKnowIdAndPassed(know.getPlanId(),
                    know.getKnowId(), 1);

            Double aLong = (double) passcount / totalcount;
            model.rate = Double.valueOf(df.format(aLong * 100));
            // 最后练习时间
            YxyHuixuePlanKnowTrain last = yxyHuixuePlanKnowTrainRepository.findTopByPlanIdAndKnowId(know.getPlanId(),
                    know.getKnowId());
            model.lastTime = sdf.format(last.getCreatedDate());
            model.quesCount = 0;
        }

        return model;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 掌握程度0已掌握1未掌握
        public Integer status;
        // 练习题目数
        public Integer quesCount;
        // 平均解题速度
        public String avgtime;
        // 解题正确率
        public Double rate;
        // 最后练习时间
        public String lastTime;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String id;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "id不能为空");
        }

    }
}
